//
//  OrderViewController.m
//  jang
//
//  Created by lee han on 23/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "OrderViewController.h"
#import "CRUserData.h"
#import "OrderCell.h"
#import "CommonView.h"
#import "PayViewController.h"

@interface OrderViewController ()

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(onClickNext:)];
    
    self.navigationItem.rightBarButtonItem = barButtonItem;
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    
    _mTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [_mTableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:@"OrderCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onClickNext:(id)sender{
    NSString* addrid = [NSString stringWithFormat:@"%ld", [CRUserData sharedInstance].mOrderAddressID];
    NSString* money = [NSString stringWithFormat:@"%ld", [[CRUserData sharedInstance] getOrderMoney]];
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 addrid,  @"addrid",
                                 [CRUserData sharedInstance].mOrderData, @"pdata",
                                 money, @"money",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_CreateOrder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row >= [[CRUserData sharedInstance].mOrderData count])
        return 30;
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

//end UITableViewDelegate

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[CRUserData sharedInstance].mOrderData count] + 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row < [[CRUserData sharedInstance].mOrderData count]){
        OrderCell *cell = [_mTableView dequeueReusableCellWithIdentifier:@"OrderCell" forIndexPath:indexPath];
        
        
        NSDictionary* rdata = [[CRUserData sharedInstance].mOrderData objectAtIndex:indexPath.row];
        NSDictionary* pdata = [[CRUserData sharedInstance].mOrderProductData objectForKey:[rdata objectForKey:@"productid"]];
        [cell initWithData:rdata productdata:pdata];
        
        return cell;
    }
    
    UITableViewCell* tcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Ordercell1"];
    CGRect tfram = tcell.frame;
    tfram.size.height = 30;
    tcell.frame = tfram;
    tcell.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [tcell setSelectionStyle:UITableViewCellSelectionStyleNone];
    tcell.textLabel.font = [UIFont systemFontOfSize:13];
    
    NSInteger productcount = [[CRUserData sharedInstance].mOrderData count];
    NSDictionary* addressinfo = [[CRUserData sharedInstance] getOrderAddress];
    if(indexPath.row == productcount){
        tcell.textLabel.text = @"姓名";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [addressinfo objectForKey:@"name"];
    }
    else if (indexPath.row == productcount + 1){
        tcell.textLabel.text = @"电话";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [addressinfo objectForKey:@"phone"];
    }
    else if (indexPath.row == productcount + 2){
        tcell.textLabel.text = @"地址";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.text = [addressinfo objectForKey:@"address"];
    }
    else if (indexPath.row == productcount + 3){
        tcell.textLabel.text = @"运费";
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        tcell.detailTextLabel.textColor = [UIColor redColor];
        tcell.detailTextLabel.text = @"以实际配送为准";
    }
    else if (indexPath.row == productcount + 4){
        tcell.textLabel.textColor = [UIColor redColor];
        tcell.textLabel.text = @"金额";
        
        tcell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        tcell.detailTextLabel.textColor = [UIColor redColor];
        tcell.detailTextLabel.text = [NSString stringWithFormat:@"￥%ld",[[CRUserData sharedInstance] getOrderMoney]];
    }
    
    return tcell;
}


//end UITableViewDataSource

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if (type == NetworkFunctionType_CreateOrder){
        if([[response objectForKey:@"status"] integerValue] == 1){
            
            //跳转到支付界面
            [[CRUserData sharedInstance].mOrderData removeAllObjects];
            
            mOrderData = [response objectForKey:@"order"];
            [self performSegueWithIdentifier:@"ordertopay" sender:self];
        }
        else{
            [CommonView addAlertView:self Text:@"创建订单错误"];
        }
    }
}
//end networkDelegate

- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender{
    PayViewController *destination = segue.destinationViewController;
    
    destination.mOrderData = mOrderData;
    destination.mType = 0;
}

@end
